
<template>
    <div>
        <div class="text-center">
            <RadioGroup v-model="formData.type">
                <Radio label="1">账期出账</Radio>
                <Radio label="2">自定义出账</Radio>
            </RadioGroup>
        </div>
        <Form ref="form" class="margin-top-15" :model="formData" :label-width="90">
            <FormItem v-if="formData.type == 2" label="账单编号" prop="billNo" :rules="required">
                <Input v-model="formData.billNo" />
            </FormItem>
            <FormItem label="客户组织" prop="orgId" :rules="required">
                <Select  v-model="formData.orgId" @on-change="getCodeList" filterable clearable>
                    <Option v-for="item in getSysAllList" :value="item.value" :key="item.value">{{item.label}}</Option>
                </Select>
            </FormItem>
            <FormItem :label="formData.type == 1 ? '账期':'自定义账期'" prop="periodId" :rules="required">
                <Select  v-model="formData.periodId" >
                    <Option v-for="item in codeList" :value="item.id" :key="item.id">{{item.code}}</Option>
                </Select>
            </FormItem>
        </Form>
        <div class="text-right">
            <Button type="primary" @click="submit">确定</Button>
            <Button class="margin-left-5" @click="cancel">取消</Button>
        </div>
    </div>
</template>

<script>
import { HttpGetCodeList,HttpCreateBillFreeze } from '@/service/api/views'
import { mapState,mapGetters } from "vuex";
export default {
    data(){
        return {
            formData:{
                type:'1',
            },
            required:[{
                required: true,
                message: "该信息不能为空,请填写"
            }],
            codeList:[],
        }
    },
    computed:{
        ...mapGetters('dictionary',['getSysAllList']),
    },
    methods:{
        getCodeList(){
            HttpGetCodeList({orgId:this.formData.orgId}).then(res=>{
                this.codeList = res.data
            })
        },
        async submit(){
            let form = await this.$refs.form.validate()
            if (!form) return;

            // 选中账期
            let chooseone = this.codeList.filter(item=>{return item.id == this.formData.periodId})[0]
            console.log(chooseone)
            this.formData.periodCode = chooseone.code;
            this.formData.startTime = chooseone.startTime;
            this.formData.endTime = chooseone.endTime;
            HttpCreateBillFreeze({
                ...this.formData
            }).then(res=>{
                this.$Message.success(res.message);
                this.cancel();
            },err=>{
                this.$Message.error({
                    content: err.message,
                    duration: 30,
                    closable: true
                });
            })
        },
        cancel(){
            this.$emit('on-close')
        }
    }
}
</script>